//
//  FilterAttributeView.m
//  图片故事
//
//  Created by 大帅逼 on 16/3/2.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "FilterAttributeView.h"
#import "FilterObject.h"
#import "FilterModel.h"
#import "Header.h"
#import "ImageProcessViewController.h"

@interface FilterAttributeView (){

    CGFloat _minValue;
    CGFloat _maxValue;
    CGFloat _defaulatValue;
    NSString *_attributeImageName;
    CGFloat   _sliderValue;
    NSInteger _index;
    NSInteger _clickBtnTag;
    
    CGFloat _contrast;
    CGFloat _saturation;
    CGFloat _exposure;
    CGFloat _white;
}

@property(nonatomic,strong) UIImageView         *imageView;
@property(nonatomic,strong) UIImage             *oldImage;
@property(nonatomic,strong) UIView              *filterAttributeView;
@property(nonatomic,strong) UIView              *sliderView;
@property(nonatomic,strong) UIImageView         *attributeImage;
@property(nonatomic,strong) UISlider            *attributeSlider;

@end

@implementation FilterAttributeView

- (instancetype)initWithFrame:(CGRect)frame oldImage:(UIImage *)oldImage imageView:(UIImageView *)imageView{

    if (self = [super initWithFrame:frame]) {
        
        _imageView = imageView;
        
        _oldImage = oldImage;
        
        _clickBtnTag = 0;
        
        [self initView];
    }
    
    return self;
}

- (void) initView {

    _contrast = 1.0;
    _saturation = 1.0;
    _exposure = 0.0;
    _white = 5000.0;
    
    // 添加滑动条视图
    [self addSubview:self.sliderView];
    
    // 添加属性选择视图
    [self addSubview:self.filterAttributeView];
}

#pragma mark -- 点击事件
- (void) action_attributeBtnClickEvent : (UIButton *) sender{
    
    if (sender.tag != _clickBtnTag && _clickBtnTag != 0) {
        
        [self showSliderView];
    }

    _clickBtnTag = sender.tag;
    
    switch (sender.tag) {
        case 120:
            
            _index = 0;
            _minValue = 0.0;
            _maxValue = 2.0;
            if (_contrast != _defaulatValue) {
                
                _defaulatValue = _contrast;
            }else {
            
                _defaulatValue = 1.0;
            }
            
            NSLog(@"点击了这里－－－》%lf,%lf",_defaulatValue,_contrast);
            
            _attributeImageName = @"contrast_icon";
            
            [ImageProcessViewController shardImagePrecessViewController].filterImage = [ImageProcessViewController shardImagePrecessViewController].getCurrImageViewImage;
            
            [self showSliderView];
            
            break;
        case 121:
            
            _index = 1;
            _minValue = 0.5;
            _maxValue = 1.5;
            if (_saturation != _defaulatValue) {
                
                _defaulatValue = _saturation;
            }else {
                
                _defaulatValue = 1.0;
            }
            _attributeImageName = @"saturation_icon";
            
            [ImageProcessViewController shardImagePrecessViewController].filterImage = [ImageProcessViewController shardImagePrecessViewController].getCurrImageViewImage;
            
            [self showSliderView];
            
            break;
        case 122:
            
            _index = 2;
            _minValue = -5.0;
            _maxValue = 5.0;

            if (_exposure != _defaulatValue) {
                
                _defaulatValue = _exposure;
            }else {
                
                _defaulatValue = 0.0;
            }
            
            _attributeImageName = @"exposure_icon";
            
            [ImageProcessViewController shardImagePrecessViewController].filterImage = [ImageProcessViewController shardImagePrecessViewController].getCurrImageViewImage;
            
            [self showSliderView];
            
            break;
            
        case 123:
            
            _index = 3;
            _minValue = 3000.0;
            _maxValue = 7000.0;
  
            if (_white != _defaulatValue) {
                
                _defaulatValue = _white;
            }else {
                
                _defaulatValue = 5000.0;
            }
            
            _attributeImageName = @"white_icon";
            
            [ImageProcessViewController shardImagePrecessViewController].filterImage = [ImageProcessViewController shardImagePrecessViewController].getCurrImageViewImage;
            
            [self showSliderView];
            
            break;
            
        default:
            break;
    }
}

#pragma mark -- 滑动条改变的事件
- (void) changeSliderValue : (UISlider *) slider {
    
//    switch (_index) {
//        case 0:
//        
//            _contrast = slider.value;
//            NSLog(@"%ld,%lf",_index,_contrast);
//            break;
//        case 1:
//            
//            _saturation = slider.value;
//            break;
//        case 2:
//            
//            _exposure = slider.value;
//            break;
//        case 3:
//            
//            _white = slider.value;
//            break;
//            
//        default:
//            break;
//    }

    [self filterWithValue:slider.value];
}

#pragma mark -- 调用滤镜对象实现对图片的处理
- (void) filterWithValue : (CGFloat) value {

    UIImage *newImage;
    
    newImage = [[FilterObject shardFilterObject] filterWithIndex:_index value:value oldImage:[ImageProcessViewController shardImagePrecessViewController].filterImage];
    
    [_imageView setImage:newImage];
}

#pragma mark -- 滑动条视图的现实和隐藏

- (void) showSliderView {

    if (self.sliderView.alpha > 0) {
        
        [UIView animateWithDuration:0.5 animations:^{
            self.sliderView.alpha = 0;
        }];
        
    }else {
    
        [_attributeSlider setValue:_defaulatValue];
        
        [_attributeSlider setMaximumValue:_maxValue];
        
        [_attributeSlider setMinimumValue:_minValue];
        
        [_attributeImage setImage:[UIImage imageNamed:_attributeImageName]];
        
        [UIView animateWithDuration:0.5 animations:^{
            self.sliderView.alpha = 1;
        }];
    }
}

#pragma mark -- 懒加载

- (UIView *)filterAttributeView {

    if (!_filterAttributeView) {
        
        _filterAttributeView = [[UIView alloc] init];
        
        [_filterAttributeView setFrame:CGRectMake(0, My_Height(45), SCREEN_W, My_Height(80))];
        
        [_filterAttributeView setBackgroundColor:[UIColor colorWithWhite:0.8 alpha:0.5]];
        
        NSArray *objects = [FilterObject readProFilterArray];
        
        for (int i = 0; i < objects.count; i++) {
            
            FilterModel *filterModel = [objects objectAtIndex:i];
            
            UIButton *attributeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            
            [attributeBtn setBounds:CGRectMake(0, 0, My_Height(40), My_Height(40))];
            
            [attributeBtn setCenter:CGPointMake(SCREEN_W / 8 * (2 * i + 1), My_Height(30))];
            
            [attributeBtn setImage:[UIImage imageNamed:filterModel.filterImage] forState:UIControlStateNormal];
            
            [attributeBtn setTag:(120 + i)];
            
            [attributeBtn addTarget:self action:@selector(action_attributeBtnClickEvent :) forControlEvents:UIControlEventTouchUpInside];
            
            [_filterAttributeView addSubview:attributeBtn];
            
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_W / 4 * i, My_Height(50), SCREEN_W / 4, My_Height(20))];
            
            [titleLabel setFont:[UIFont systemFontOfSize:12]];
            
            [titleLabel setTextColor:[UIColor whiteColor]];
            
            [titleLabel setTextAlignment:NSTextAlignmentCenter];
            
            [titleLabel setText:filterModel.filterTitle];
            
            [_filterAttributeView addSubview:titleLabel];
        }
    }
    
    return _filterAttributeView;
}

- (UIView *)sliderView {

    if (!_sliderView) {
        
        _sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, My_Height(45))];
        
        [_sliderView setAlpha:0];
        
        [_sliderView setBackgroundColor:[UIColor blackColor]];
        
        // 添加图标控件和滑动条
        _attributeImage = [[UIImageView alloc] initWithFrame:CGRectMake(My_Height(20), My_Height(12.5), My_Height(20), My_Height(20))];
        
        [_sliderView addSubview:_attributeImage];
        
        _attributeSlider = [[UISlider alloc] initWithFrame:CGRectMake(My_Height(60), My_Height(12.5), SCREEN_W - My_Height(80), 20)];
        
        [_attributeSlider addTarget:self action:@selector(changeSliderValue :) forControlEvents:UIControlEventValueChanged];
        
        [_attributeSlider setThumbImage:[UIImage imageNamed:@"sliderTint"] forState:UIControlStateNormal];
        
        [_attributeSlider setThumbImage:[UIImage imageNamed:@"sliderTint"] forState:UIControlStateSelected];
        
        [_sliderView addSubview:_attributeSlider];
    }
    return _sliderView;
}

@end
